import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fim/util/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

class EditImage extends StatefulWidget {
  final String title;
  late Widget image = Image.asset("assets/add.png");
  final void Function(File) onPicked;

  final _picker = ImagePicker();

  EditImage({Key? key, required this.title, required String url, required this.onPicked})
      : super(key: key) {
    if (url != null && url != "") {
      image = CachedNetworkImage(imageUrl: url);
    }
  }

  @override
  _EditImageState createState() => _EditImageState();
}

class _EditImageState extends State<EditImage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 30, right: 30),
      height: 70,
      child: Row(
        children: [
          Text(widget.title, style: TextStyle(fontSize: 18)),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(top: 10, bottom: 10),
              alignment: Alignment.centerRight,
              child: GestureDetector(
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: widget.image,
                ),
                onTap: () {
                  logger.i("我想替换图片");
                  _getImage();
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  Future _getImage() async {
    final pickedFile =
        await widget._picker.pickImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      var _croppedFile = await _cropImage(pickedFile.path);
      if (_croppedFile != null) {
        final path = _croppedFile.path;
        setState(() {
          widget.image = Image.file(File(path));
        });
        widget.onPicked(File(path));
      }
    } else {
      logger.i('No image selected.');
    }
  }

  Future<CroppedFile?> _cropImage(String path) async {
    // CroppedFile? croppedFile = (await ImageCropper().cropImage(
    //   // cropStyle: CropStyle.rectangle,
    //   sourcePath: path,
    //   maxWidth: 200,
    //   maxHeight: 200,
    //   aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
    //   aspectRatioPresets: !kIsWeb && Platform.isAndroid
    //       ? [
    //     CropAspectRatioPreset.square,
    //     CropAspectRatioPreset.ratio3x2,
    //     CropAspectRatioPreset.original,
    //     CropAspectRatioPreset.ratio4x3,
    //     CropAspectRatioPreset.ratio16x9,
    //   ]
    //       : [
    //     CropAspectRatioPreset.original,
    //     CropAspectRatioPreset.square,
    //     CropAspectRatioPreset.ratio3x2,
    //     CropAspectRatioPreset.ratio4x3,
    //     CropAspectRatioPreset.ratio5x3,
    //     CropAspectRatioPreset.ratio5x4,
    //     CropAspectRatioPreset.ratio7x5,
    //     CropAspectRatioPreset.ratio16x9,
    //   ],
    // ));
    //
    // if (croppedFile != null) {
    //   return croppedFile;
    // }

    final croppedFile = await ImageCropper().cropImage(
      sourcePath: path,
      compressFormat: ImageCompressFormat.jpg,
      compressQuality: 100,
      uiSettings: [
        AndroidUiSettings(
          toolbarTitle: 'Cropper',
          toolbarColor: Colors.deepOrange,
          toolbarWidgetColor: Colors.white,
          initAspectRatio: CropAspectRatioPreset.square,
          lockAspectRatio: false,
          aspectRatioPresets: [
            CropAspectRatioPreset.original,
            CropAspectRatioPreset.square,
            CropAspectRatioPreset.ratio4x3,
            CropAspectRatioPresetCustom(),
          ],
        ),
        IOSUiSettings(
          title: 'Cropper',
          aspectRatioPresets: [
            CropAspectRatioPreset.original,
            CropAspectRatioPreset.square,
            CropAspectRatioPreset.ratio4x3,
            CropAspectRatioPresetCustom(),
          ],
        ),
        WebUiSettings(
          context: context,
          presentStyle: WebPresentStyle.dialog,
          size: const CropperSize(
            width: 200,
            height: 200,
          ),
        ),
      ],
    );

    if (croppedFile != null) {
      return croppedFile;
    }

    return null;
  }
}

class CropAspectRatioPresetCustom implements CropAspectRatioPresetData {
  @override
  (int, int)? get data => (2, 3);

  @override
  String get name => '2x3 (customized)';
}
